package cn.com.nes.site.service.fault.impl;

import cn.com.nes.common.em.DictTypeEnum;
import cn.com.nes.mybatis.agent.ope.entity.IsEquipmentFaultInfo;
import cn.com.nes.mybatis.agent.ope.mapper.IsEquipmentFaultInfoMapper;
import cn.com.nes.mybatis.agent.system.entity.TParam;
import cn.com.nes.mybatis.agent.system.mapper.TParamMapper;
import cn.com.nes.site.entity.PageData;
import cn.com.nes.site.entity.dto.fault.*;
import cn.com.nes.site.entity.em.GzztEnum;
import cn.com.nes.site.entity.vo.fault.EquipmentFaultInfoListVo;
import cn.com.nes.site.entity.vo.fault.FaultVo;
import cn.com.nes.site.entity.vo.fault.UpdateFaultStateVo;
import cn.com.nes.site.service.fault.FaultService;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

@Service
public class FaultServiceImpl implements FaultService {

    @Resource
    IsEquipmentFaultInfoMapper isEquipmentFaultInfoMapper;

    @Resource
    TParamMapper paramMapper;

    @Override
    public PageData getEquipmentFaultInfoList(EquipmentFaultInfoListVo vo) throws Exception {
        Page<EquipmentFaultInfoListDto> page = new Page<>(vo.getPageid(), vo.getPagesize());
        List<EquipmentFaultInfoListDto> dtos = isEquipmentFaultInfoMapper.getEquipmentFaultInfoList(page,vo);

        // 到字典表查询故障处理名称
        List<TParam> dictList = paramMapper.getDictByParamType(DictTypeEnum.FAULT_STATE.getParamType());
        Map<Integer,String> dict = new HashMap<>();
        if (!CollectionUtils.isEmpty(dictList)) {
            for (TParam tParam : dictList) {
                dict.put(Integer.valueOf(tParam.getValue()),tParam.getName());
            }
        }

        // 利用迭代器给每个故障设置故障状态名称
        Iterator<EquipmentFaultInfoListDto> iterator = dtos.iterator();
        while (iterator.hasNext()) {
            EquipmentFaultInfoListDto dto = iterator.next();
            Integer stateCode = dto.getGzqk();
            String stateName = "";
            if (null != stateCode)
                stateName = dict.get(stateCode);
            dto.setGzztmc(stateName);
        }
        return PageData.ok(page, dtos);
    }

    @Override
    public List<GetFaultTrendDto> getFaultTrend(FaultVo vo) {
        List<GetFaultTrendDto> dtos = isEquipmentFaultInfoMapper.getFaultTrend(vo);
        //把天数转化为时间戳
        for (GetFaultTrendDto dto : dtos) {
            dto.setDatetime(dto.getDatetime()*24*60*60*1000);
        }
        return dtos;
    }

    @Override
    public FaultInfoDto getFaultInfoById(String faultId) {
        //根据故障id获取其故障详细信息
        FaultInfoDto dto  = isEquipmentFaultInfoMapper.getFaultInfoById(faultId);
        if(ObjectUtil.isNotNull(dto.getJd()) && ObjectUtil.isNotNull(dto.getWd()) && dto.getJd() !=0 && dto.getWd() !=0){
            Double[] lnglat =new Double[]{dto.getJd(),dto.getWd()};
            dto.setLnglat(lnglat);
        }
        //判断如果处理状态为未处理则将处理时间置为空,处理模式置空
        if(GzztEnum._1.getKey().equals(dto.getState().toString())){
            dto.setHandleTime(null);
            dto.setErroMode(null);
        }
        //如果设备没有别名则显示为设备类型名称-设备型号
        if(StrUtil.isBlank(dto.getSbbm())){
            String sblxmc = dto.getSblxmc();
            String sbxh = dto.getSbxh();
            dto.setSbbm(sblxmc+"-"+sbxh);
        }
        return dto;
    }

    @Override
    public int updateFaultState(UpdateFaultStateVo vo) {
        String sjly = "agent";
        //根据业务id查询数据
        IsEquipmentFaultInfo equipmentFaultInfo = isEquipmentFaultInfoMapper.selectEquipmentFaultInfoByFaultId(vo.getFaultId());
        //封装数据
        equipmentFaultInfo.setUserId(vo.getUserId());
        equipmentFaultInfo.setErroDesc(vo.getDesc());
        equipmentFaultInfo.setState((byte)0);
        equipmentFaultInfo.setErroMode(vo.getType().byteValue());
        equipmentFaultInfo.setXgsj(null);
        equipmentFaultInfo.setSjly(sjly);
        //更新故障数据
        int res = isEquipmentFaultInfoMapper.updateById(equipmentFaultInfo);
        return res;
    }

    @Override
    public List<GetFaultByProduct> getFaultByProduct(FaultVo vo) {
        List<GetFaultByProduct> dtos =  isEquipmentFaultInfoMapper.getFaultByProduct(vo);
        //统计每种设备型号的故障总数
        for (GetFaultByProduct dto : dtos) {
            Integer sum = 0;
            for (FaultList fault : dto.getFaultList()) {
                sum += fault.getValue();
            }
            dto.setCount(sum);

        }
        return dtos;
    }

    @Override
    public List<GetFaultByType> getFaultByFaultType(FaultVo vo) {
        List<GetFaultByType> dtos =  isEquipmentFaultInfoMapper.getFaultByFaultType(vo);
        //统计每种设备型号的故障总数
        for (GetFaultByType dto : dtos) {
            Integer sum = 0;
            for (FaultList fault : dto.getProdList()) {
                sum += fault.getValue();
            }
            dto.setCount(sum);

        }
        if (vo.getState() != null && vo.getState() > -1) {
            dtos.sort((o1, o2) -> o2.getCount().compareTo(o1.getCount()));
            dtos = dtos.subList(0, Math.min(dtos.size(), 10));
        }
        return dtos;
    }
}
